import { Controller, Get, Post, Body } from '@nestjs/common';
import { ApiUseTags, ApiOperation, ApiModelProperty } from '@nestjs/swagger';
import { User } from './user.model';
import { IsNotEmpty } from 'class-validator';
import bodyParser = require('body-parser');
import { InjectModel } from 'nestjs-typegoose';
import { ModelType } from '@hasezoey/typegoose/lib/types';

class CreateUserDto {
  @ApiModelProperty({ description: '用户名', example: 'jsoh'})
  @IsNotEmpty({ message: '请填写用户名'})
  username: string
  @ApiModelProperty({ description: '密码', example: '123456'})
  @IsNotEmpty({ message: '请填写密码'})
  password: string
  @ApiModelProperty({ description: '邮箱', example: 'xxx@163.com'})
  @IsNotEmpty({ message: '请填写邮箱'})
  email: string
  @ApiModelProperty({ description: '手机号码', example: '150xxxx1636'})
  phone: string
}

@Controller('users')
@ApiUseTags('用户')
export class UsersController {
  constructor(
    @InjectModel(User) private readonly userModel: ModelType<User>
  ) {}
  @Get()
  @ApiOperation({ title: '用户列表'})
  async users() {
    return await this.userModel.find()
  }
  
  @Post()
  @ApiOperation({ title: '创建用户' })
  async createUser(@Body() CreateUserDto: CreateUserDto) {
    await this.userModel.create(CreateUserDto)
    return {
      success: true
    }
  }
}

